<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form v-loading="formLoading" :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <!-- <el-form-item label="父id" prop="parentId">
      <el-input v-model="dataForm.parentId" placeholder="父id"></el-input>
    </el-form-item>  -->
    <el-form-item  label="父级字典" prop="parentId">
     <el-popover 
          ref="menuListPopover"
          placement="bottom-start"
          trigger="click">
          <el-tree
            :data="menuList"
            :props="menuListTreeProps"
            node-key="id"
            ref="menuListTree"
            @current-change="menuListTreeCurrentChangeHandle"
            :default-expand-all="true"
            :highlight-current="true"
            :expand-on-click-node="false">
          </el-tree>
        </el-popover>
        <el-input disabled=""  v-model="dataForm.parentName" v-popover:menuListPopover :readonly="true" placeholder="点击选择上级菜单" class="menu-list__input"></el-input>
    </el-form-item>
    <!-- <el-form-item label="字典名称" prop="name">
      <el-input v-model="dataForm.name" placeholder="字典名称"></el-input>
    </el-form-item> -->
    <el-form-item label="字典类型" prop="type">
      <el-input v-model="dataForm.type" placeholder="字典类型" ></el-input>
    </el-form-item>
    <!-- <el-form-item label="字典码" prop="code">
      <el-input v-model="dataForm.code" placeholder="字典码"></el-input>
    </el-form-item> -->
    <el-form-item label="字典值" prop="value">
      <el-input v-model="dataForm.value" placeholder="字典值"></el-input>
    </el-form-item>
    <el-form-item label="排序" prop="orderNum">
      <el-input v-model="dataForm.orderNum" placeholder="排序"></el-input>
    </el-form-item>
    <!-- <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item> -->
    
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" v-loading="formLoading" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { treeDataTranslate } from '@/utils'
  export default {
    data () {
      return {
        visible: false,
        formLoading: false,
         demoVisible: false,
           cid:'',
        dataForm: {
          id: 0,
          parentId: '',
          parentName:'',
          name: '',
          type: '',
          code: '',
          value: '',
          orderNum: '',
          remark: '',
          version: '',
        },
        dataRule: {
          // parentId: [
          //   { required: true, message: '父级字典不能为空', trigger: 'change' }
          // ],
          name: [
            { required: true, message: '字典名称不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '字典类型不能为空', trigger: 'blur' }
          ],
          value: [
            { required: true, message: '字典值不能为空', trigger: 'blur' }
          ],
          orderNum: [
            { required: true, message: '排序不能为空', trigger: 'blur' }
          ],
 
        },
          menuList: [],
         multipleSelection: [],
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        key: '',
        menuListTreeProps: {
          label: 'name',
          children: 'children'
        }
      }
    },
    methods: {
      init (id,pid,type,code,cid) {
       
        this.dataForm.id = id || 0
        this.$http({
          url: this.$http.adornUrl('/sys/dict/list2'),
          method: 'get',
          params: this.$http.adornParams()
        }).then(({data}) => {
       
          this.menuList = treeDataTranslate(data, 'id')
        }).then(() => {
        this.visible = true
        this.formLoading = false

        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
           })
          }).then(() => {
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/sys/dict/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                debugger;
                this.dataForm.parentId = data.dict.parentId
                this.dataForm.name = data.dict.name
                this.dataForm.type = data.dict.type
                this.dataForm.code = data.dict.code
                this.dataForm.value = data.dict.value
                this.dataForm.orderNum = data.dict.orderNum
                this.dataForm.remark = data.dict.remark
                this.dataForm.version = data.dict.version
                  this.menuListTreeSetCurrentNode()
              }
            })
          }else{
            
        debugger
        var newtype="";
         if(pid!=null&&pid!=''){
            this.dataForm.parentId = pid
            
              if(cid == '0'){
            newtype = type+"_00";
            }else if(type!=null&&type!=""){
          /*newtype = node[0].type+"_"+parseInt(node[0].type.substring(node[0].type.length-2,node[0].type.length))+1;*/
            newtype = type+"_"+code;
           }
          this.dataForm.type = newtype; 
           this.menuListTreeSetCurrentNode()
         }
          
          }
        })
      },
       // 菜单树选中
      menuListTreeCurrentChangeHandle (data, node) {
      debugger;
        this.dataForm.parentId = data.id
        this.dataForm.parentName = data.name

        var newtype="";
        if(this.dataForm.parentId == '0'){
            newtype = data.type+"_00";
        }else if(data.type!=null&&data.type!=""){
          /*newtype = node[0].type+"_"+parseInt(node[0].type.substring(node[0].type.length-2,node[0].type.length))+1;*/
            newtype = data.type+"_"+data.code;
          }
        this.dataForm.type = newtype;  
      },
      // 菜单树设置当前选中节点
      menuListTreeSetCurrentNode () {
        debugger
        this.$refs.menuListTree.setCurrentKey(this.dataForm.parentId)
        this.dataForm.parentName = (this.$refs.menuListTree.getCurrentNode() || {})['name']
      },
      // 图标选中
      iconActiveHandle (iconName) {
        this.dataForm.icon = iconName
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid && !this.formLoading) {
            this.formLoading = true
            this.$http({
              url: this.$http.adornUrl(`/sys/dict/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'parentId': this.dataForm.parentId,
                'name': this.dataForm.name,
                'type': this.dataForm.type,
                'code': this.dataForm.code,
                'value': this.dataForm.value,
                'orderNum': this.dataForm.orderNum,
                'remark': this.dataForm.remark,
                'version': this.dataForm.version
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.formLoading = false
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
